package com.kfm.order.service.impl;

import cn.hutool.core.util.IdUtil;
import com.kfm.common.domain.entity.OrderDetails;
import com.kfm.common.domain.model.Sale;
import com.kfm.common.domain.model.Saleroom;
import com.kfm.order.service.OrderDetailsService;
import org.aspectj.weaver.ast.Or;
import org.springframework.stereotype.Service;

import org.springframework.beans.factory.annotation.Autowired;

import com.kfm.common.domain.entity.Orders;
import com.kfm.order.mapper.OrdersMapper;
import com.kfm.order.service.OrdersService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class OrdersServiceImpl implements OrdersService {

    @Resource
    private OrdersMapper ordersMapper;

    @Resource
    private OrderDetailsService detailsService;

    @Override
    public int deleteByPrimaryKey(String orderId) {
        return ordersMapper.deleteByPrimaryKey(orderId);
    }

    @Override
    public int insert(Orders record) {
        record.setOrderId(IdUtil.simpleUUID());
        return ordersMapper.insert(record);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSelective(Orders record, List<OrderDetails> list) {
        // 使用雪花算法生成唯一 id
        String id = IdUtil.getSnowflakeNextIdStr();
        record.setOrderId(id);
        list.forEach(e -> e.setOrderId(id));
        detailsService.batchInsert(list);
        return ordersMapper.insertSelective(record);
    }

    @Override
    public Orders selectByPrimaryKey(String orderId) {
        return ordersMapper.selectByPrimaryKey(orderId);
    }

    @Override
    public int updateByPrimaryKeySelective(Orders record) {
        return ordersMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Orders record) {
        return ordersMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<Orders> selectByMId(Integer mid) {
        return ordersMapper.selectByMId(mid);
    }

    @Override
    public List<Orders> selectAll() {
        return ordersMapper.selectAll();
    }

    @Override
    public List<Orders> selectByCondition(Map<String, String> map) {
        return ordersMapper.selectByCondition(map);
    }

    @Override
    public List<Sale> selectSales() {
        return ordersMapper.selectSales();
    }

    @Override
    public List<Saleroom> selectSaleroom() {
        return ordersMapper.selectSaleroom();
    }

}
